package org.jeecg.modules.alarmRecording.utils;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class CustomTimer {
    private final ScheduledExecutorService executor;

    public CustomTimer() {
        executor = Executors.newSingleThreadScheduledExecutor();
    }

    public void startTimer() {
        executor.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                // 在定时器内部做判断
                if (conditionSatisfied()) {
                    stopTimer();
                } else {
                    // 执行定时任务
                    System.out.println("定时任务执行中...");
                }
            }
        }, 0, 1, TimeUnit.SECONDS); // 每隔1秒执行一次
    }

    public void stopTimer() {
        executor.shutdown();
    }

    private boolean conditionSatisfied() {
        // 判断条件是否满足，如果满足返回true，否则返回false
        return true; // 例如：假设条件满足
    }

    public static void main(String[] args) {
        CustomTimer timer = new CustomTimer();
        timer.startTimer();
    }
}
